<template>
  <div class="login">
    <div class="title">
      <router-link to="/home" tag="div" class="back">
        <span class="iconfont icon-fanhuijiantou"></span>
      </router-link>
    </div>

    <div class="log">
      <div class="icon">
        <span class="iconfont icon-shu"></span>
      </div>
    </div>
    <!-- 
    <div class="input_wrap">
      <input type="text" placeholder="手机号" />
    </div>
    <div class="input_wrap">
      <input type="password" placeholder="密码" />
    </div> -->

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
      />
      <div class="agreement">
        <van-checkbox v-model="checked" icon-size="12px" checked-color="#ee0a24"
          >同意用户协议、隐私政策</van-checkbox
        >
      </div>

      <div style="margin: 16px">
        <van-button round block color="#FD9291" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>

    <div class="registration">
      <span>验证码登录</span>
      <router-link to="/registered" tag="span">新用户注册</router-link>
      <span>忘记密码</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      checked: false,
    };
  },
  methods: {
    onSubmit() {
      if (window.localStorage.getItem("token")) {
        let { name, password } = JSON.parse(
          window.localStorage.getItem("token")
        );
        if (this.checked) {
          if (this.username == name) {
            if (this.password == password) {
              this.$toast.loading({
                message: "登录中...",
                forbidClick: true,
                duration: 500, //登录延迟时间
              });
              this.$router.push("/myhome");
            } else {
              this.$toast("账号或密码不正确！");
            }
          } else {
            this.$toast("账号或密码不正确！");
          }
        } else {
          this.$dialog
            .confirm({
              title: "同意服务协议及隐私保护",
            })
            .then(() => {
              this.checked = true;
            })
            .catch(() => {
              this.checked = false;
            });
        }
      }else{
       this.$toast("账号未注册");
      }

      //   let user = {
      //     name:this.username,
      //     password:this.password
      //   }
      //   window.localStorage.setItem("token",JSON.stringify(user))
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  .registration {
    display: flex;
    justify-content: center;
    span {
      padding: 0 8px;
      border-right: 1px solid #ccc;
      font-size: 14px;
      color: #808080;
      &:last-child {
        border-right: none;
      }
    }
  }
  .van-cell {
    width: 311px;
    margin: 10px auto;
    background-color: #f9f9f9;
    border-radius: 45px;
    &::after {
      position: relative;
    }
  }

  .van-button {
    width: 311px;
    margin: 0 auto;
  }
  .agreement {
    display: flex;
    margin-top: 20px;
    justify-content: center;
    padding-left: 16px;
  }
  .title {
    display: flex;
    .back {
      height: 44px;
      display: flex;
      padding-left: 16px;
      align-items: center;
      span {
        text-align: center;
        font-weight: 700;
        font-size: 24px;
      }
    }
  }
  .log {
    .icon {
      height: 180px;
      align-items: center;
      display: flex;
      justify-content: center;
      span {
        font-size: 80px;
        border: 1px solid #edd388;
        border-radius: 50%;
        color: #edd388;
      }
    }
  }
  .input_wrap {
    width: 285px;
    height: 48px;
    margin: 10px auto;
    background-color: #f9f9f9;
    border-radius: 48px;
    display: flex;
    align-items: center;
    padding-left: 18px;
    padding-right: 5px;
    input {
      background-color: #f9f9f9;
      height: 23px;
      font-size: 18px;
      width: 100%;
      border: none;
    }
  }
}
</style>